package cn.ivicar.oss.resource;

import cn.com.carsmart.exception.CarsmartException;
import cn.com.carsmart.ws.util.CacheUtil;
import cn.ivicar.oss.controller.CustomerController;
import cn.ivicar.oss.controller.SimInfoController;
import cn.ivicar.oss.controller.VehicleController;
import cn.ivicar.oss.entity.ResponseEntity;
import com.google.inject.Inject;
import com.google.inject.servlet.RequestScoped;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

/**
 * 与sim有关的接口
 * Created by Administrator on 2017/5/21.
 */
@Path("simInfo")
@RequestScoped
public class SimInfoResource {
    private static transient final Logger LOGGER = LoggerFactory.getLogger(SimInfoResource.class);

    @Inject
    private SimInfoController simInfoController;
    @Inject
    private CustomerController customerController;
    @Inject
    private VehicleController vehicleController;

    /*
    通过iccid查找sim信息
     */
    @Path("data")
    @GET
    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public Response getSimInfo(@QueryParam("iccid") String iccid) throws CarsmartException {

        JSONObject simInfo = this.simInfoController.getSimInfoByIccid(iccid);
        JSONObject result = new JSONObject();
        result.put("data", simInfo);
        return CacheUtil.noCache(Response.ok(result)).build();
    }

    /*
    查找所有sim信息
     */
    @Path("datas")
    @GET
    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public Response getSimInfos() throws CarsmartException {

        JSONArray simInfoList = this.simInfoController.getSimInfoList();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("data", simInfoList);
        return CacheUtil.noCache(Response.ok(jsonObject)).build();
    }

    /*
    通过iccid查找车辆sim信息
     */
    @Path("vehicles")
    @GET
    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public Response getVehicles(@QueryParam("iccid") String iccid) throws CarsmartException {

        JSONArray vehicles = this.simInfoController.getVehicles(iccid);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("data", vehicles);
        jsonObject.put("type", "simToVehicle");

        return CacheUtil.noCache(Response.ok(jsonObject)).build();
    }

    /*
    通过iccid查找车辆，用户，sim信息
     */
    @Path("cvs")
    @GET
    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public Response getCVSRelation(@QueryParam("iccid") String iccid) throws CarsmartException {

        JSONObject content = new JSONObject();
        if (StringUtils.isNotBlank(iccid)) {
            JSONObject simInfo = this.simInfoController.getSimInfoByIccid(iccid);
            //暂转为数组
            content.put("vehicle", new String[] {});
            content.put("customer", new String[] {});
            content.put("simInfo", new String[] {});
            if (simInfo != null) {
                String[] sim = new String[8];
                sim[0] = simInfo.optString("iccid");
                sim[1] = simInfo.optString("userCode");
                sim[2] = simInfo.optString("operator");
                sim[3] = simInfo.optString("carStatus");
                sim[4] = simInfo.optString("smsStatus");
                sim[5] = simInfo.optString("effectTime");
                sim[6] = simInfo.optString("orgId");
                sim[7] = simInfo.optString("created");
                content.put("simInfo", sim);
            }

            Long vid = this.simInfoController.getVehicleIdByIccid(iccid);
            if (vid != null) {
                JSONObject vehicle = this.vehicleController.getVehicleById(Long.valueOf(vid));
                //暂转为数组
                if (vehicle != null) {
                    String[] v = new String[7];
                    v[0] = vehicle.optString("vid");
                    v[1] = vehicle.optString("name");
                    v[2] = vehicle.optString("serial");
                    v[3] = vehicle.optString("plate");
                    v[4] = vehicle.optString("vin");
                    v[5] = vehicle.optString("ein");
                    v[6] = vehicle.optString("created");
                    content.put("vehicle", v);
                }

                Long cid = this.vehicleController.getCustomerIdByVid(Long.valueOf(vid));
                if (cid != null) {
                    JSONObject customer = this.customerController.getCustomerById(cid);
                    if (customer != null) {
                        String[] c = new String[7];
                        c[0] = customer.optString("id");
                        c[1] = customer.optString("email");
                        c[2] = customer.optString("mobile");
                        c[3] = customer.optString("nickName");
                        c[4] = customer.optString("sex");
                        c[5] = customer.optString("birthday");
                        c[6] = customer.optString("created");
                        content.put("customer", c);
                    }
                }
            }
        }

        return CacheUtil.noCache(Response.ok(content)).build();
    }

    /*
    删除对应车辆sim信息
     */
    @Path("vehicle")
    @DELETE
    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public Response deleteVehicleRel(@QueryParam("iccid") String iccid,
        @QueryParam("vid") String vid) throws CarsmartException {
        ResponseEntity responseEntity = this.simInfoController.deleteVehicle(iccid, vid);
        return CacheUtil.noCache(Response.ok(responseEntity)).build();
    }

    /*
    新增车辆sim信息
     */
    @Path("vehicle")
    @POST
    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public Response addVehicleRel(JSONObject jsonObject) throws CarsmartException {
        ResponseEntity responseEntity = this.simInfoController.addVehicleRel(jsonObject);
        return CacheUtil.noCache(Response.ok(responseEntity)).build();
    }

    /*
    获得车辆状态（开机，关机）
     */
    @Path("index/deviceStatus")
    @GET
    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public Response getDeviceStatus() throws CarsmartException {
        JSONArray deviceStatus = this.simInfoController.getDeviceStatus();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("data", deviceStatus);
        return CacheUtil.noCache(Response.ok(jsonObject)).build();
    }

    /*
    获得用户状态
     */
    @Path("index/userStatus")
    @GET
    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public Response getUserStatus() throws CarsmartException {
        JSONArray userStatus = this.simInfoController.getUserStatus();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("data", userStatus);
        return CacheUtil.noCache(Response.ok(jsonObject)).build();
    }

    /*
    获得7天流量信息
     */
    @Path("index/sevenFlow")
    @GET
    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public Response getSevenFlow() throws CarsmartException {
        JSONObject jsonObject = this.simInfoController.getSevenFlow();
        return CacheUtil.noCache(Response.ok(jsonObject)).build();
    }

    /*
    获得月流量排行
     */
    @Path("index/mongthRank")
    @GET
    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public Response getMongthRank() throws CarsmartException {
        JSONArray mongthRank = this.simInfoController.getMongthRank();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("data", mongthRank);
        return CacheUtil.noCache(Response.ok(jsonObject)).build();
    }

    /*
    获得新增sim信息
     */
    @Path("newList")
    @GET
    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public Response getNewSimList() throws CarsmartException {
        JSONArray mongthRank = this.simInfoController.getNewSimList();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("data", mongthRank);
        return CacheUtil.noCache(Response.ok(jsonObject)).build();
    }

    /*
    获得sim消费信息
     */
    @Path("simConsumeList")
    @GET
    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public Response getSimConsumeList(@QueryParam("iccid") String iccid) throws CarsmartException {
        JSONArray simConsumeList = this.simInfoController.getSimConsumeList(iccid);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("data", simConsumeList);
        return CacheUtil.noCache(Response.ok(jsonObject)).build();
    }

    /*
    获得每日sim信息
     */
    @Path("simDailyStat")
    @GET
    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public Response getSimDailyStat(@QueryParam("iccid") String iccid) throws CarsmartException {
        JSONObject jsonObject = this.simInfoController.getSimDailyStat(iccid);
        return CacheUtil.noCache(Response.ok(jsonObject)).build();
    }

    /*
    获得没月sim信息
     */
    @Path("simMonthyStat")
    @GET
    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public Response getSimMonthyStat(@QueryParam("iccid") String iccid) throws CarsmartException {
        JSONObject jsonObject = this.simInfoController.getSimMonthyStat(iccid);
        return CacheUtil.noCache(Response.ok(jsonObject)).build();
    }

    /*
    新增sim信息
     */
    @Path("add")
    @POST
    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public Response addSimInfo(JSONObject jsonObject) throws CarsmartException {
        ResponseEntity responseEntity = this.simInfoController.addSimInfo(jsonObject);
        return CacheUtil.noCache(Response.ok(responseEntity)).build();
    }

    /*
    更新sim信息
     */
    @Path("GPRS")
    @POST
    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public Response changeGPRS(JSONObject jsonObject) throws CarsmartException {
        ResponseEntity responseEntity = this.simInfoController.changeGPRS(jsonObject);
        return CacheUtil.noCache(Response.ok(responseEntity)).build();
    }

    /*
    更新车辆状态
     */
    @Path("CarStatus")
    @POST
    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public Response changeCarStatus(JSONObject jsonObject) throws CarsmartException {
        ResponseEntity responseEntity = this.simInfoController.changeCarStatus(jsonObject);
        return CacheUtil.noCache(Response.ok(responseEntity)).build();
    }

    /*
    获得对应sim缴费信息
     */
    @Path("chargeRecord")
    @GET
    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public Response chargeRecord(@QueryParam("iccid") String iccid) throws CarsmartException {
        JSONArray jsonArray = this.simInfoController.chargeRecord(iccid);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("data", jsonArray);
        return CacheUtil.noCache(Response.ok(jsonObject)).build();
    }

    /*
    获得套餐信息
     */
    @Path("packAgeInfo")
    @GET
    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public Response packAgeInfo(@QueryParam("packageId") String packageId)
        throws CarsmartException {
        JSONObject jsonObject = this.simInfoController.packAgeInfo(packageId);
        return CacheUtil.noCache(Response.ok(jsonObject)).build();
    }

}
